package bg.unisofia.fmi.tis.ui.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.ListDataModel;

import bg.unisofia.fmi.tis.model.Comment;
import bg.unisofia.fmi.tis.model.Notification;
import bg.unisofia.fmi.tis.model.Thesis;
import bg.unisofia.fmi.tis.model.ThesisStatus;
import bg.unisofia.fmi.tis.model.User;
import bg.unisofia.fmi.tis.service.ModelService;

public class UserBean extends BackingBeanBase {

	private User user;
	private ListDataModel notificationsModel = new ListDataModel();
	
	private Map<Thesis, Boolean> canUpdateThesis = new HashMap<Thesis, Boolean>() {
		public Boolean get(Object key) {
			if (key instanceof Thesis) {
				Thesis thesis = (Thesis) key;
				return thesis.getStatus() == ThesisStatus.ACCEPTED;
			}
			
			return Boolean.FALSE;
		}
	};
	
	public Map<Thesis, Boolean> getCanUpdateThesis() {
		return canUpdateThesis;
	}

	
	private Map<Thesis, String> lastComment = new HashMap<Thesis, String>() {
		public String get(Object key) {
			
			String commentStr = "";
			
			if (key instanceof Thesis) {
				Thesis thesis = (Thesis) key;
				Comment comment = ModelService.getLastComment(thesis);
				
				if (comment != null) {
					commentStr = 
						comment.getProfessor().getName() + ": " + 
							comment.getContent() + " // " + 
							comment.getSubmittedOn();
				}
			}
			return commentStr;
		}
	};
	public Map<Thesis, String> getLastComment() {
		return lastComment;
	}

	
	private Map<Thesis, Boolean> isReviewed = new HashMap<Thesis, Boolean>() {
		public Boolean get(Object key) {
			if (key instanceof Thesis) {
				Thesis thesis = (Thesis) key;
				return thesis.getStatus() == ThesisStatus.REVIEWED;
			}
			
			return Boolean.FALSE;
		}
	};
	public Map<Thesis, Boolean> getIsReviewed() {
		return isReviewed;
	}

	
	
	public void update(User user) {
		this.user = user;
		
		List<Notification> notifications = ModelService.loadNotifications(user);
		getNotificationsModel().setWrappedData(notifications);
	}

	public String actionSubmitProposal() {
		
		ThesisBean bean = getManagedBean(ThesisBean.class);
		bean.update(new Thesis(), user);
		
		return "proposalSubmit";
	}
	
	public String actionReviewThesis() {
		
		Notification notification = (Notification) getNotificationsModel().getRowData();
		
		Thesis thesis = notification.getForThesis();
		
		ThesisBean bean = getManagedBean(ThesisBean.class);
		bean.update(thesis, user);
		
		if (thesis.getStatus() == ThesisStatus.PENDING) {
			return "thesisReview";
		} else if (thesis.getStatus() == ThesisStatus.PROPOSED) {
			return "thesisFinalReview";
		}
		
		return "";
	}
	
	public String actionUpdateThesis() {
		
		Notification notification = (Notification) getNotificationsModel().getRowData();
		
		ThesisBean bean = getManagedBean(ThesisBean.class);
		bean.update(notification.getForThesis(), user);
		
		return "thesisUpdate";
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public ListDataModel getNotificationsModel() {
		return notificationsModel;
	}

	public void setNotificationsModel(ListDataModel notificationsModel) {
		this.notificationsModel = notificationsModel;
	}
}
